package com.tpshion.cloud.base.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tpshion.cloud.base.domain.dto.ConfigInfoDto;
import com.tpshion.cloud.base.domain.dto.UpdateConfigDto;
import com.tpshion.cloud.base.domain.entity.ConfigInfo;
import com.tpshion.cloud.base.domain.vo.ConfigInfoVo;
import com.tpshion.cloud.base.mapper.ConfigInfoMapper;
import com.tpshion.cloud.base.service.ConfigInfoService;
import com.tpshion.cloud.common.support.Assert;
import com.tpshion.cloud.common.support.ErrorCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ConfigInfoServiceImpl implements ConfigInfoService {

    @Autowired
    private ConfigInfoMapper configInfoMapper;

    /**
     * 分页查询配置信息
     * @param configInfoDto
     * @return
     */
    @Override
    public PageInfo queryConfig(ConfigInfoDto configInfoDto) {
        PageHelper.startPage(configInfoDto.getPageNum(),configInfoDto.getPageSize());
        List<ConfigInfoVo> list = configInfoMapper.queryConfig(configInfoDto.getDataId(), configInfoDto.getGroupId());
        return new PageInfo(list);
    }

    /**
     * 保存修改配置
     * @param updateConfigDto
     */
    @Override
    @Transactional
    public void updateConfig(UpdateConfigDto updateConfigDto) {
        ConfigInfo configInfo = configInfoMapper.selectById(updateConfigDto.getId());
        Assert.notNull(configInfo, ErrorCode.FAIL.getCode(),"配置信息不存在");
        configInfo.setContent(updateConfigDto.getContent());
        configInfo.setGmtModified(LocalDateTime.now());
        configInfoMapper.updateById(configInfo);
    }
}
